// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.assets.service.impl;

import com.liuxinlong.common.AosException;
import com.liuxinlong.common.SnowFlake;
import com.liuxinlong.constant.ThreadLocalConstants;
import com.liuxinlong.constant.TimeFormatConstants;
import com.liuxinlong.enums.ReturnCodes;
import com.liuxinlong.modules.assets.service.AssetsInvestmentProjectService;
import com.liuxinlong.modules.dao.AssetsInvestmentDao;
import com.liuxinlong.modules.dao.AssetsInvestmentProjectDao;
import com.liuxinlong.modules.dao.ProjectDao;
import com.liuxinlong.modules.dao.UserDao;
import com.liuxinlong.modules.entity.AssetsInvestment;
import com.liuxinlong.modules.entity.AssetsInvestmentProject;
import com.liuxinlong.modules.entity.User;
import com.liuxinlong.utils.ObjectUtils;
import com.liuxinlong.utils.ThreadlocalUtils;
import com.liuxinlong.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 固资测算项目管理业务接口层实现
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2024-10-17
 */
@Service
@Slf4j
public class AssetsInvestmentProjectServiceImpl implements AssetsInvestmentProjectService {

    @Autowired
    private AssetsInvestmentProjectDao assetsInvestmentProjectDao;

    @Autowired
    private AssetsInvestmentDao assetsInvestmentDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private ProjectDao projectDao;

    @Override
    public List<Map<String, Object>> pageAssetsInvestmentProjectInfo(Map<String, Object> queryParam) {
        List<AssetsInvestmentProject> originalList = assetsInvestmentProjectDao.pageAssetsInvestmentProject(queryParam);
        int startNum = (int) queryParam.get("startNum");
        return completeResult(originalList, startNum);
    }

    @Override
    public int getAssetsInvestmentProjectCount(Map<String, Object> queryParam) {
        return assetsInvestmentProjectDao.getAssetsInvestmentProjectCount(queryParam);
    }

    @Override
    public Map<String, Object> getAssetsInvestmentDetail(String id) {
        Map<String, Object> dataMap = new HashMap<>();

        return dataMap;
    }

    @Override
    @Transactional
    public void addAssetsInvestmentProject(AssetsInvestmentProject assetsInvestmentProject) {
        User currentUser = (User) ThreadlocalUtils.getCache(ThreadLocalConstants.USER_INFO);
        String currentTime = TimeUtils.getDate(TimeFormatConstants.YYYY_MM_DD_HH_MM_SS);
        String id = SnowFlake.nextIdStr();
        AssetsInvestment assetsInvestment = assetsInvestmentDao.selectById(assetsInvestmentProject.getInvestmentId());
        if (ObjectUtils.isEmpty(assetsInvestment)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR,"测算数据不存在！");
        }
        if (assetsInvestment.getCalculationType() == 2) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR,"测算数据非项目数据，转项目失败！");
        }
        assetsInvestmentProject.setProjectId(assetsInvestment.getProjectId());
        assetsInvestmentProject.setId(id);
        assetsInvestmentProject.setCreateUser(currentUser.getId());
        assetsInvestmentProject.setCreateTime(currentTime);
        assetsInvestmentProjectDao.insert(assetsInvestmentProject);
        assetsInvestment.setStatus(3);
        assetsInvestment.setUpdateTime(currentTime);
        assetsInvestment.setUpdateUser(currentUser.getId());
        assetsInvestmentDao.updateById(assetsInvestment);
    }

    @Override
    public void updateAssetsInvestmentProject(AssetsInvestmentProject assetsInvestmentProject) {
        User currentUser = (User) ThreadlocalUtils.getCache(ThreadLocalConstants.USER_INFO);
        String currentTime = TimeUtils.getDate(TimeFormatConstants.YYYY_MM_DD_HH_MM_SS);
        assetsInvestmentProject.setUpdateUser(currentUser.getId());
        assetsInvestmentProject.setUpdateTime(currentTime);
        assetsInvestmentProjectDao.updateById(assetsInvestmentProject);
    }

    @Override
    public void deleteAssetsInvestmentProject(String id) {

    }


    /**
     * 补全数据
     *
     * @param originalList 原始数据
     * @param startNum     数据起始值
     * @return 补全数据列表
     */
    private List<Map<String, Object>> completeResult(List<AssetsInvestmentProject> originalList, int startNum) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        Map<String, Map<String, String>> userMap = userDao.getUserMap();
        Map<String,Map<String,String>> projectMap = projectDao.getProjectMap();
        int sort = startNum + 1;
        for (AssetsInvestmentProject investmentProject : originalList) {
            Map<String, Object> item = ObjectUtils.object2Map(investmentProject);
            item.put("sort", sort);
            Map<String,String> projectInfo = projectMap.get(investmentProject.getProjectId());
            item.put("projectName",projectInfo.get("name"));
            item.put("projectManageName", userMap.containsKey(projectInfo.get("projectManager")) ?
                    userMap.get(projectInfo.get("projectManager")).get("name") : "");
            resultList.add(item);
            sort++;
        }
        return resultList;
    }

}
